package com.bdzl.biz.controller;

import java.io.*;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.aliyun.oss.HttpMethod;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.aliyun.oss.model.OSSObject;
import com.bdzl.pub.controller.BaseController;
import com.bdzl.pub.util.file.DownFileUtil;
import com.bdzl.pub.util.file.OSSClientFactory;
import com.bdzl.pub.vo.loan.OrderArchiveVO;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bdzl.pub.entity.loan.OrderArchiveDO;
import com.bdzl.biz.service.OrderArchiveService;
import com.bdzl.pub.util.param.PageUtils;
import com.bdzl.pub.util.param.Query;
import com.bdzl.pub.util.param.R;

import javax.servlet.http.HttpServletResponse;

/**
 * 贷款主订单归档信息表
 *
 * @author yangyicai
 * @email yangyicai@foxmail.com
 * @date 2021-05-11 15:02:10
 */

@Controller
@RequestMapping("/bdzl/orderArchive")
public class OrderArchiveController extends BaseController {
	@Autowired
	private OrderArchiveService orderArchiveService;

	@GetMapping()
	String OrderArchive(){
	    return "bdzl/orderArchive/orderArchive";
	}

	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<OrderArchiveDO> orderArchiveList = orderArchiveService.list(query);
		int total = orderArchiveService.count(query);
		PageUtils pageUtils = new PageUtils(orderArchiveList, total);
		return pageUtils;
	}

	@GetMapping("/add")
	String add(){
	    return "bdzl/orderArchive/add";
	}

	@GetMapping("/edit/{archiveId}")
	String edit(@PathVariable("archiveId") Integer archiveId,Model model){
		OrderArchiveDO orderArchive = orderArchiveService.get(archiveId);
		model.addAttribute("orderArchive", orderArchive);
	    return "bdzl/orderArchive/edit";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( OrderArchiveDO orderArchive){
		if(orderArchiveService.save(orderArchive)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( OrderArchiveDO orderArchive){
		orderArchiveService.update(orderArchive);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Integer archiveId){
		if(orderArchiveService.remove(archiveId)>0){
		return R.ok();
		}
		return R.error();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Integer[] archiveIds){
		orderArchiveService.batchRemove(archiveIds);
		return R.ok();
	}


	/**
	 *  下载文件 zip格式
	 * @param url
	 * @param fileName
	 */
	@GetMapping("/downloadArchive")
	@ResponseBody
	public String downloadArchive(String url,String fileName){
		try {
			return DownFileUtil.downLoadFromUrlHttps(url,fileName).toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "下载失败";
	}

	/**
	 * 档案管理
	 */
	@ResponseBody
	@GetMapping("/listAll")
	public PageUtils listAll(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<OrderArchiveVO> orderArchiveList = orderArchiveService.listAll(query);
		int total = orderArchiveService.listAllCount(query);
		PageUtils pageUtils = new PageUtils(orderArchiveList, total);
		return pageUtils;
	}


}
